#include "IO.h"
#include "BSP_SPI.h"
#include "BSP_Delay.h"
#include "BSP_Config.h"

static const IO_T W25Q_CS = IO_INIT_TABLE(W25Q_CS_GP, W25Q_CS_PIN);

static void W25Q_GPIO_Init(void){	
	IO_Init(&W25Q_CS, IO_Mode_OUT);
	IO_Set(&W25Q_CS, 1);
}


void W25Q_IO_Init(void){
	W25Q_GPIO_Init();
}

unsigned char W25Q_TxRxByte(unsigned char TxByte)
{
	return BSP_SPI_Exchange(W25Q_SpiId, TxByte);
}



void W25Q_ChipSelect(unsigned int PinLevel)
{
  IO_Set(&W25Q_CS, PinLevel );
  //BSP_SPI_CS(!PinLevel);
}



static void W25W_DelayUnit(void){
#if 0
	volatile uint32_t i;
	for(i = 0; i < 1; i++){

	}
#endif 
	BSP_Delay_us(1);
}

static volatile unsigned int w25qTimeoutCount;

void W25Q_TimeoutStart(unsigned int waitTime){
	w25qTimeoutCount = waitTime;
}


int W25Q_IsTimeout(void){
	if(w25qTimeoutCount > 0) {
		w25qTimeoutCount--;
		W25W_DelayUnit();
		return 0;
	}
	return 1;
}



